package com.haidechizi.fastadmin.modules.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.haidechizi.fastadmin.common.annotation.DataFilter;
import com.haidechizi.fastadmin.common.page.PageDto;
import com.haidechizi.fastadmin.common.utils.PageInfo;
import com.haidechizi.fastadmin.modules.sys.dao.SysRoleDao;
import com.haidechizi.fastadmin.modules.sys.entity.SysOffice;
import com.haidechizi.fastadmin.modules.sys.entity.SysRole;
import com.haidechizi.fastadmin.modules.sys.service.SysOfficeService;
import com.haidechizi.fastadmin.modules.sys.service.SysRoleMenuService;
import com.haidechizi.fastadmin.modules.sys.service.SysRoleOfficeService;
import com.haidechizi.fastadmin.modules.sys.service.SysRoleService;
import com.haidechizi.fastadmin.modules.sys.service.SysUserRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;


/**
 * 角色
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements SysRoleService {
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysRoleOfficeService sysRoleOfficeService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysOfficeService sysOfficeService;


    @Override
    @DataFilter(subOffice = true, user = false)
    public PageInfo queryPage(PageDto pageDto, SysRole sysRole) {
        Wrapper<SysRole> wrapper;
        String roleName = sysRole.getRoleName();
        Page<SysRole> page = this.selectPage(
                pageDto.cover2Page(),
                wrapper = new EntityWrapper<SysRole>()
                        .like(StringUtils.isNotBlank(roleName), "role_name", roleName)
                        .addFilterIfNeed(pageDto.getSqlFilter() != null, pageDto.getSqlFilter())
        );
        for (SysRole role : page.getRecords()) {
            SysOffice sysOffice = sysOfficeService.selectById(role.getOfficeId());
            if (sysOffice != null) {
                role.setOfficeName(sysOffice.getName());
            }
        }
        int count = super.selectCount(wrapper
                .addFilterIfNeed(pageDto.getSqlFilter() != null, pageDto.getSqlFilter()));
        page.setTotal(count);

        return new PageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysRole role) {
        role.setCreateTime(new Date());
        this.insert(role);

        //保存角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

        //保存角色与部门关系
        sysRoleOfficeService.saveOrUpdate(role.getRoleId(), role.getOfficeIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRole role) {
        this.updateById(role);

        //更新角色与菜单关系
        sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());

        //保存角色与部门关系
        sysRoleOfficeService.saveOrUpdate(role.getRoleId(), role.getOfficeIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        //删除角色
        this.deleteBatchIds(Arrays.asList(roleIds));

        //删除角色与菜单关联
        sysRoleMenuService.deleteBatch(roleIds);

        //删除角色与部门关联
        sysRoleOfficeService.deleteBatch(roleIds);

        //删除角色与用户关联
        sysUserRoleService.deleteBatch(roleIds);
    }


}
